from typing import List

# O(N)

class Solution:
    def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
        ans = 0
        j = 0
        for i in range(len(nums1)):
            while j + 1 < len(nums2) and nums2[j + 1] >= nums1[i]:
                j += 1
            if nums1[i] <= nums2[j]:
                ans = max(ans, j - i)
            if j == len(nums2) - 1:
                break
        return ans


if __name__ == "__main__":
    print(Solution().maxDistance(nums1=[55, 30, 5, 4, 2], nums2=[100, 20, 10, 10, 5]))  # 2
    print(Solution().maxDistance(nums1=[2, 2, 2], nums2=[10, 10, 1]))  # 1
    print(Solution().maxDistance(nums1=[30, 29, 19, 5], nums2=[25, 25, 25, 25, 25]))  # 2
    print(Solution().maxDistance(nums1=[5, 4], nums2=[3, 2]))  # 0
